package db

import (
	"github.com/jinzhu/gorm"
	"log"
	//_ "github.com/jinzhu/gorm/dialects/mysql"
	_ "github.com/jinzhu/gorm/dialects/postgres"
	"git.oschina.net/fanbuchi/xgggh/oauth2/model"
)
var mMaster *gorm.DB
func init() {
	//mysql:="root:qw123@tcp(192.168.249.210:3306)/oauth2?charset=utf8&parseTime=True&loc=Local"
	//db, err := gorm.Open("mysql",mysql)
	postgres:="postgresql://root@localhost:26257/oauth2?sslmode=disable"
	//postgres:="postgresql://root@192.168.249.210:26257/oauth2?sslmode=disable"

	db, err := gorm.Open("postgres",postgres)
	if err !=nil{
		log.Fatal(err)
	}
	db.AutoMigrate(&model.Account{})
	db.AutoMigrate(&model.Scope{})
	db.AutoMigrate(&model.Role{})
	db.AutoMigrate(&model.AccessToken{})
	db.AutoMigrate(&model.AuthorizationCode{})
	db.AutoMigrate(&model.Client{})
	db.AutoMigrate(&model.RefreshToken{})

	mMaster=db
	//defer db.Close()
}

func GetDB() *gorm.DB{
	return mMaster.Debug()
}
